package cn.script.game;

import cn.script.domain.Point;
import cn.script.domain.RangePoint;
import cn.script.opt.WinOpt;
import cn.script.util.LogOut;

/**
 * 游戏信息获取
 *
 * @author 周永波
 */
public class GameInfo {

    private WinOpt wio;

    public WinOpt wio() {
        return this.wio;
    }

    public GameInfo(WinOpt wio) {
        this.wio = wio;
    }

    /**
     * 当前游戏的坐标
     *
     * @return
     */
    public Point nowCd() {
        String cdStr = this.wio.ocr("f8e000", RangePoint.rp(652, 48, 706, 61));
        if (cdStr == null || "".equals(cdStr)) {
            return null;
        }
        String[] split = cdStr.split(",");
        return Point.point(Integer.valueOf(split[0]), Integer.valueOf(split[1]));
    }

    /**
     * 当前游戏所在城市
     *
     * @return
     */
    public String nowCity() {
        String location = this.wio.ocr("f8e000", RangePoint.rp(620, 21, 740, 45));
        if (location == null || "".equals(location)) {
            return null;
        }
        return location;
    }

    /**
     * 是否打开背包
     *
     * @return
     */
    public boolean isOpenBackpack() {
        String back = this.wio.ocr("f8e000", RangePoint.rp(654, 491, 765, 505));
        if (back == null || "".equals(back) || !"整理摆摊".equals(back)) {
            return false;
        }
        return true;
    }

    /**
     * 金钱
     *
     * @return
     */
    public long money() {
        if (!this.isOpenBackpack()) {
            this.wio.keypress("b", 200);
        }
        String ret = this.wio.ocr("98f060", RangePoint.rp(560, 462, 675, 478));
        this.wio.keypress("b", 100);
        if (ret == null || "".equals(ret)) {
            return 0;
        }
        return Long.valueOf(ret);
    }

    /**
     * 判断城市
     *
     * @param city
     * @return
     */
    public boolean isCity(String city) {
        String ret = this.nowCity();
        if (ret == null || !ret.equals(city)) {
            return false;
        }
        return true;
    }

    /**
     * 是否打开了回城按钮
     *
     * @return
     */
    public boolean isOpenBack() {
        String ret = this.wio.ocr("f8e000", RangePoint.rp(455, 293, 508, 310));
        if (ret == null || !ret.equals("凌霄城")) {
            return false;
        }
        return true;
    }

    /**
     * 是否打开设置
     *
     * @return
     */
    public boolean isReset() {
        String ret = this.wio.ocr("e8e8b8", RangePoint.rp(358, 149, 455, 162));
        if (ret == null || !ret.equals("系统设置")) {
            return false;
        }
        return true;
    }

    /**
     * 是否打开地图
     *
     * @return
     */
    public boolean isOpenMap() {
        String ret = this.wio.ocr("e8f090-000000|e8ec90-000000", RangePoint.rp(402, 478, 417, 493));
        if (ret == null || !ret.equals(">")) {
            return false;
        }
        return true;
    }

    /**
     * 打开地图
     */
    public void openMap() {
        if (this.isOpenMap()) {
            return;
        }
        this.wio.keypress("tab", 200);
    }

    /**
     * 获取当前锁定人物或者怪物的名称
     *
     * @return
     */
    public String attackName() {
        String ret = this.wio.ocr("00fc00-000000|f81440-000000|f0f4a0-000000", RangePoint.rp(270, 38, 447, 55));
        if (ret == null || ret.equals("")) {
            return null;
        }
        return ret;
    }

    /**
     * 获取当前生命值
     *
     * @return
     */
    public int liveVal() {
        String ret = this.wio.ocr("f8e000", RangePoint.rp(332, 60, 365, 71));
        if (ret == null || ret.equals("")) {
            return -1;
        }
        return Integer.valueOf(ret.replace("%", ""));
    }

    /**
     * 背包是否已满
     *
     * @return
     */
    public boolean isBackpackMeet() {
        String ret = this.wio.ocr("f8e000", RangePoint.rp(366, 256, 436, 271));
        if (ret == null || ret.equals("")) {
            return false;
        }
        if ("我知道了".equals(ret)) {
            LogOut.out("背包已满");
            return true;
        }
        return false;
    }
}
